const {
    pro,
    str_status,
    timeStr
} = require('../common/pro_mysql');
const {
    createToken,
    verifyToken
} = require('../common/jwt_token');

var type_admin = 0;

// 获取商品列表
async function GetProductList(req,res){
    // type 0 浏览量排序 1 价格升序  2价格降序 3 销量升序 4 销量降序 5按照关键词keyword搜索
    let {type = '0',page = 0,limit = 10,keyword = "",admin_id} = req.query;
    let sql = '';
    switch (type) {
        case '0':
            sql = `select * from product order by duck_look asc limit ${page*limit || 0},${limit || 10}`
            break;
        case '1':
            sql = `select * from product order by duck_min_price asc limit ${page*limit || 0},${limit || 10}`;
            break;
        case '2':
            sql = `select * from product order by duck_min_price desc limit ${page*limit || 0},${limit || 10}`;
            break;
        case '3':
            sql = `select * from product order by duck_hot asc limit ${page*limit || 0},${limit || 10}`;
            break;
        case '4':
            sql = `select * from product order by duck_hot desc limit ${page*limit || 0},${limit || 10}`;
            break;
        default: 
            res.send(str_status(0,'参数错误',[]))
            return false
    }
    if(admin_id){
        sql = sql.replace("order", "where admin_id="+admin_id+" order");
        if(keyword != ""){
            sql = sql.replace("order", "and duck_name like '%"+keyword+"%' order");
        }
    }else{
        if(keyword != ""){
            sql = sql.replace("order", "where duck_name like '%"+keyword+"%' order");
        }
    }
    let {result} = await pro(sql);
    console.log(result);
    if(type_admin != 0){
        return result
    }
    res.send(str_status(1,'获取成功',result))
}

// 获取轮播图 
async function GetBanner(req,res){
    let {
        admin_id
    } = req.query;
    let sql = `select * from banner where admin_id=${admin_id}`;
    let {result} = await pro(sql);
    if(type_admin != 0){
        return result
    }
    res.send(str_status(1,'',result))
}

// 获取所有分类
async function GetCategary(req,res){
    let {
        admin_id
    } = req.query;
    console.log(admin_id);
    let sql = `select * from categary_small where admin_id=${admin_id}`;
    let {result} = await pro(sql);
    if(type_admin != 0){
        return result
    }
    res.send(str_status(1,'',result))
}


// 首页数据
async function GetHome(req,res){
    let {
        admin_id
    } = req.query;
    if(!admin_id){
        res.status(400).send(str_status(0,'你要谁的数据呀,admin_id呢?',[]));
        return
    }
    type_admin = 10;
    let banner_list = await GetBanner({query:{admin_id}});
    let categary_list = await GetCategary({query:{admin_id}});
    let product_list =await GetProductList({query:{admin_id}});
    res.send(
        str_status(1,'',{
            banner_list,
            categary_list,
            product_list
        })
    ) 
}


// 获取组合分类接口
async function GetCategory(req,res){
    let {
        admin_id
    } = req.query;
    if(!admin_id){
        res.status(400).send(str_status(0,'你的admin_id'))
        return
    }
    let sql = `
        SELECT 
        c.ID AS categary_id, 
        c.name AS categary_name, 
        c.url AS categary_url, 
        GROUP_CONCAT(JSON_OBJECT('categary_url', cs.url,'categary_id', cs.ID, 'categary_name', cs.name) SEPARATOR ',') AS categary_list
    FROM 
        categary AS c
        LEFT JOIN categary_small AS cs ON c.ID = cs.categary_id
    WHERE
        c.admin_id = ${admin_id} and cs.admin_id = ${admin_id}
    GROUP BY 
        c.ID
    `;
    let data = await pro(sql);
    res.send(data)
}

// 获取商品详情
async function GetProductDesc(req,res){
    let {
        admin_id,
        product_id
    } = req.query;

    if(!admin_id || !product_id ){
        res.status(400).send(str_status(0,'参数错误'))
        return
    }
    let sql = `select * from product where admin_id=${admin_id} and ID=${product_id}`;
    let data = await pro(sql);
    res.send(data)
}
module.exports = {
    GetProductList,
    GetHome,
    GetCategory,
    GetProductDesc
}